// pages/visaInfo/visaInfo.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');

var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {

        movies:[
            {
                url:'https://swcj.majiangyun.com/img/ioc/vist-detail.png',
            }
        ],
        setMeal:[

        ],
        clickId:0,
        clickItem:'签证费+服务费',
        flag:true,
        toView:'',
        curId:0,
        booksTile: ['商品详情', '图文介绍','商品推荐'],
        flag3:true,
        flag2: true,
        goods:{},
        list:[],
        coupon:"领取",
        couponList:[],
        specification:"",
        money:0.0,
        userInfo: {},
        isShow:false,
        clickCurr:0,
        ismore:"展开",
        isShow:'全部展开',
        curr1:'隐藏',
        isNow:false,
        preText:{},
        text2:{}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;

        var id = options.id;
        var userInfo = util.getKey("userInfo");
        this.setData({
            userInfo: userInfo
        })
        this.getModel(id);
        this.getList(id);
        this.getGoodsCouponList(id);

    },
    getSpecification: function (goods) {
        var that = this;
        httpUtil.getHttp("/goods_travel/get/" + goods.id + "/" + goods.goodsType + "/specification", {}, function (res) {


            that.setData({
                setMeal: res,
                specification:res[0].name,
                money: res[0].money
            })
        })
    },
    getModel: function (id) {
        var that = this;
        httpUtil.getHttp("/goods_travel/get/" + id + "?userId=" + that.data.userInfo.id
            , {}, function (res) {
                if (res.urlList == null || res.urlList == []) {
                    res.urlList = [res.url];
                }
                that.getSpecification(res);
                let data = app.towxml.toJson(res.text, 'markdown');
                let preText = app.towxml.toJson(res.preText, 'markdown');
                let text2 = app.towxml.toJson(res.refundText, 'markdown');


                that.setData({
                    goods: res,
                    article: data,
                    preText: preText,
                    text2: text2
                })

            })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    }, linkMore: function (e) {
        var id = e.target.dataset.id;
        console.log(id);
        if (id == undefined) {
            return;
        }
        wx.redirectTo({
            url: '../visaInfo/visaInfo?id=' + id
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    getList: function (id) {
        var that = this;
        httpUtil.getHttp("/goods_travel/list?type=0&id=" + id, {}, function (res) {
            that.setData({
                list: res
            })
        })
    },
    getGoodsCouponList:function(id){

        var that = this;
        httpUtil.getHttp("/coupon/get/goods/" + id, {}, function (res) {
            that.setData({
                couponList: res
            })
        })
    },

    selActive:function(res){
        var that=this;
        that.setData({
            clickId: res.currentTarget.dataset.id,
            specification: that.data.setMeal[res.currentTarget.dataset.id].name,
            money: that.data.setMeal[res.currentTarget.dataset.id].money

        })
    },
    clickSure:function(){
        var that = this;
        that.setData({
            flag: true
        })
    },
    showMeal: function () {
        var that = this;

        that.setData({
            flag:false
        })

    },
    gotopay:function(){
        wx.navigateTo({
            url: '../purchase/purchase'
        })
    },
    receiveVou:function(e){
        var that=this;
        var id = e.target.dataset.id;

        var userInfo = util.getKey("userInfo");

        httpUtil.getHttp("/coupon/add/goods/"+id + "/" + userInfo.id , {}, function (res) {
            if(res.id==''){
                that.setData({
                    coupon:"已领取"
                })
                wx.showToast({
                    title: '重复领取',
                    icon: 'succes',
                    duration: 1000,
                    mask: true
                })
            } else if(res.id != ''){
                wx.showToast({
                    title: '领取成功',
                    icon: 'succes',
                    duration: 1000,
                    mask: true
                })
            }else{
                wx.showToast({
                    title: '领取失败',
                    icon: 'succes',
                    duration: 1000,
                    mask: true
                })
            }
        })

    },
    clickScroll:function(e){
        console.log(e);
        var id = e.currentTarget.id
        var item = e.currentTarget.dataset.item
        this.setData({
            toView: id,
            curId:item
        })
    },
    linkCar:function(){
        wx.switchTab({
            url: '../shopCar/shopCar'
        })
    },
    linkEval:function(){
        wx.navigateTo({
            url: '../evaluate/evaluate'
        })
    },
    morexz:function(){
        var that=this;
        that.setData({
            flag3:!that.data.flag3
        })
    },
    moretg:function(){
        var that = this;
        that.setData({
            flag2: !that.data.flag2
        })
    }, evalDetail: function (e) {
        var id = e.target.dataset.id;
        var type = e.target.dataset.type;
        if (id == undefined) {
            return;
        }
        wx.navigateTo({
            url: '../evaluate/evaluate?id=' + id + "&type=" + 0
        })
    },

    ismoreDeatil:function(e){
        var id = e.currentTarget.dataset.idx;
        this.setData({
            clickCurr: id,
        })
    },
    isOpen:function (e){
        var index= e.currentTarget.dataset.item;
        console.log(index);
        var flag=true;
        this.setData({
            curr1: index,
        })
    },
    isHide: function (e) {
        this.setData({
            curr1: -1,
        })
    }
})